package helpers

import (
	"crypto/tls"
	"crypto/x509"
	"google.golang.org/grpc/credentials"
	"io/ioutil"
)

//这里的文件路径有点迷糊
//验证服务端的证书
func GetServerCert() credentials.TransportCredentials {
	cert, _ := tls.LoadX509KeyPair("./tls/server.pem", "./tls/server.key")
	certPool := x509.NewCertPool()
	ca, _ := ioutil.ReadFile("./tls/ca.pem")
	certPool.AppendCertsFromPEM(ca)
	creds := credentials.NewTLS(&tls.Config{
		Certificates: []tls.Certificate{cert}, //服务端证书
		ClientAuth:   tls.RequireAndVerifyClientCert,
		ClientCAs:    certPool,
	})

	return creds;
}

//验证客户端的证书
func GetClientCert() credentials.TransportCredentials {
	cert, _ := tls.LoadX509KeyPair("./tls/client.pem", "./tls/client.key")
	certPool := x509.NewCertPool()
	ca, _ := ioutil.ReadFile("./tls/ca.pem")
	certPool.AppendCertsFromPEM(ca)
	creds := credentials.NewTLS(&tls.Config{
		Certificates: []tls.Certificate{cert}, //客户端证书
		ServerName:   "joyceyi.top",
		RootCAs:      certPool,
	})

	return creds
}
